/*
   Copyright 2011-2012 gtalent2@gmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package main

import (
	"../models"
	eutil "../util"
	"github.com/gtalent/starfish/util"
)

const ext = ".gob"
const width = 22
const height = 25

var read, write = func() (func(string, interface{}) error, func(string, interface{}) error) {
	switch ext {
	case ".gob":
		return eutil.ReadGob, eutil.WriteGob
	case ".json":
		return eutil.ReadJSON, eutil.WriteJSON
	}
	return nil, nil
}()

func buildDirtAnimation() models.Animation {
	var a models.Animation
	a.Interval = 2
	var img models.Image
	img.Size.Width = 70
	img.Size.Height = 70
	img.Path = "../dirt.png"
	a.Images = append(a.Images, img)
	return a
}

func buildBoxmanAnimation() models.Animation {
	var a models.Animation
	a.Interval = 2
	var img models.Image
	img.Size.Width = 70
	img.Size.Height = 70
	img.Path = "../boxman.png"
	a.Images = append(a.Images, img)
	return a
}

func buildTileClass() models.TileClass {
	var tc models.TileClass
	var al models.AnimLayer
	al.Animation = buildDirtAnimation()
	al.Point = util.Point{0, 0}
	tc.LowerAnims = append(tc.LowerAnims, al)
	tc.TerrainFlags = 1
	return tc
}

func buildUser() models.Sprite {
	var s models.Sprite
	s.Speed = 2
	var al models.AnimLayer
	al.Animation = buildBoxmanAnimation()
	s.AnimLayers = make([][]models.AnimLayer, 1)
	s.AnimLayers[0] = append(s.AnimLayers[0], al)
	return s
}

func buildTile() models.Tile {
	var t models.Tile
	t.TileClass.Import = "tileclass1" + ext
	return t
}

func buildZoneHeader(width, height int) models.ZoneHeader {
	var zs models.ZoneHeader
	zs.Path = "zone1" + ext
	zs.Size = util.Size{width, height}
	return zs
}

func buildZone(width, height int) models.Zone {
	var z models.Zone
	z.Tiles = make([][][]models.Tile, 1)
	z.Tiles[0] = make([][]models.Tile, width)
	for i, _ := range z.Tiles[0] {
		l := make([]models.Tile, height)
		for ii, _ := range l {
			l[ii] = buildTile()
		}
		z.Tiles[0][i] = l
	}
	return z
}

func buildWorld() models.World {
	var w models.World
	var c models.ZoneInstance
	c.Path = "zoneheader1" + ext
	c.Location = util.Point{0, 0}
	w.Zones = append(w.Zones, c)
	return w
}

func writeTileClass() {
	z := buildTileClass()
	write("tileclass1"+ext, &z)
}

func writeZoneHeader() {
	z := buildZoneHeader(70*width, 70*height)
	write("zoneheader1"+ext, &z)
}

func writeZone() {
	z := buildZone(width, height)
	write("zone1"+ext, &z)
}

func writeWorld() {
	z := buildWorld()
	write("world1"+ext, &z)
}

func writeDemo() {
	writeTileClass()
	writeZoneHeader()
	writeZone()
	writeWorld()
}
